package com.ywh.train.gui;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.ParseException;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.text.MaskFormatter;

import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

import tk.mystudio.ocr.OCR;

import com.ywh.train.Config;
import com.ywh.train.Constants;
import com.ywh.train.ResManager;
import com.ywh.train.Util;
import com.ywh.train.bean.UserInfo;
import com.ywh.train.logic.LogicThread;
import com.ywh.train.logic.TrainClient;

public class RobTicket {
    private JFrame frame;
    //private JLabel lblRc;
    private JTextField txtUsername;
    private JTextField txtPassword;
    //private JTextField txtRandCode;
    private JTextField txtFromStation;
    private JTextField txtToStation;
    private JTextField txtStartDate;
    private JComboBox boxoRang;
    private JTextField txtBeginTime;
    private JTextField txtEndTime;
    private JTextField txtTrainCodeMatch;
    private JTextField txtBlackTrainCodes;
    private JTextField txtSeatTypes;
    private JComboBox boxoSeat;
    private JTextField txtTrainCode;
    private JTextField txtUserID1;
    private JTextField txtName1;
    private JTextField txtPhone1;
    private JTextField txtUserID2;
    private JTextField txtName2;
    private JTextField txtPhone2;
    private JTextField txtUserID3;
    private JTextField txtName3;
    private JTextField txtPhone3;
    private JTextField txtUserID4;
    private JTextField txtName4;
    private JTextField txtPhone4;
    private JTextField txtUserID5;
    private JTextField txtName5;
    private JTextField txtPhone5;
    private JCheckBox boxkDFirst;
    private JCheckBox boxkHardSleepFirst;
    private JCheckBox boxkIsAuto;
    private JCheckBox boxkEnforce;
    private JButton btnSORE;
    private JTextArea textArea;
    private HttpClient httpClient = null;
    private TrainClient client = null;
    private LogicThread logic;
    private JPanel panel;
    private JPanel panel_1;
    private JPanel panel_2;
    private JPanel panel_3;
    UserInfo ui = new UserInfo();

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    RobTicket window = new RobTicket();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public RobTicket() {
        initNetwork();
        initialize();
        Runtime.getRuntime().addShutdownHook(new ExitThread());
    }

    public void initNetwork() {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            Scheme sch = new Scheme("https", 443, ssf);
            ThreadSafeClientConnManager tcm = new ThreadSafeClientConnManager();
            tcm.setMaxTotal(10);
            tcm.getSchemeRegistry().register(sch);
            this.httpClient = new DefaultHttpClient(tcm);
            if (Config.isUseProxy()) {
                HttpHost proxy = new HttpHost(Config.getProxyIp(), Config.getProxyPort(), "http");
                this.httpClient.getParams().setParameter("http.route.default-proxy", proxy);
            }
            this.client = new TrainClient(this.httpClient);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void initialize() {
        this.frame = new JFrame(ResManager.getString("RobTicket.frameName"));
        ImageIcon ico = ResManager.createImageIcon("logo.jpg");
        this.frame.setIconImage(ico.getImage());
        this.frame.setBounds(100, 100, 505, 685);
        this.frame.setResizable(false);
        this.frame.setDefaultCloseOperation(3);
        this.frame.getContentPane().setLayout(null);
        this.frame.setLocationRelativeTo(null);
        ToolTipManager.sharedInstance().setInitialDelay(0);

        // 登录信息
        this.panel = new JPanel();
        this.panel.setBounds(0, 0, 500, 50);
        this.frame.getContentPane().add(this.panel);
        this.panel.setLayout(null);
        this.panel.setBorder(new TitledBorder(ResManager.getString("RobTicket.panelBorderName")));

        JLabel label = new JLabel(ResManager.getString("RobTicket.txtUsername"));
        label.setBounds(20, 25, 43, 12);
        this.panel.add(label);
        label.setHorizontalAlignment(4);
        this.txtUsername = new JTextField();
        //this.txtUsername.setToolTipText(ResManager.getString("RobTicket.txtUsernameTip"));
        this.txtUsername.setBounds(65, 21, 85, 20);
        this.panel.add(this.txtUsername);
        this.txtUsername.setColumns(10);

        JLabel label_1 = new JLabel(ResManager.getString("RobTicket.txtPassword"));
        label_1.setBounds(165, 25, 38, 12);
        this.panel.add(label_1);
        label_1.setHorizontalAlignment(4);
        this.txtPassword = new JPasswordField();
        //this.txtPassword.setToolTipText(ResManager.getString("RobTicket.txtPasswordTip"));
        this.txtPassword.setBounds(205, 21, 100, 20);
        this.panel.add(this.txtPassword);
        this.txtPassword.setColumns(10);

        //JLabel label_2 = new JLabel(ResManager.getString("RobTicket.txtRandCode"));
        //label_2.setBounds(320, 25, 48, 12);
        //this.panel.add(label_2);
        //label_2.setHorizontalAlignment(4);
        //this.txtRandCode = new JTextField();
        //this.txtRandCode.setToolTipText(ResManager.getString("RobTicket.txtRandCodeTip"));
        //this.txtRandCode.setBounds(370, 21, 53, 20);
        //this.panel.add(this.txtRandCode);
        //this.txtRandCode.setColumns(10);
        //this.lblRc = new JLabel("click me!");
        //this.lblRc.setToolTipText(ResManager.getString("RobTicket.lblRcTip"));
        //this.lblRc.setBounds(425, 20, 65, 22);
        //this.panel.add(this.lblRc);
        //this.lblRc.addMouseListener(new RandCodeListener());

        // 车次信息
        this.panel_1 = new JPanel();
        this.panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), ResManager.getString("RobTicket.panel_1BorderTitle"), 4, 2, null,
                new Color(0, 0, 0)));
        this.panel_1.setBounds(0, 60, 500, 80);
        this.frame.getContentPane().add(this.panel_1);
        this.panel_1.setLayout(null);

        JLabel txtFromStationl = new JLabel(ResManager.getString("RobTicket.txtFromStation"));
        txtFromStationl.setBounds(20, 25, 33, 12);
        this.panel_1.add(txtFromStationl);
        txtFromStationl.setHorizontalAlignment(4);
        this.txtFromStation = new JTextField();
        //this.txtFromStation.setToolTipText(ResManager.getString("RobTicket.txtFromStationTip"));
        this.txtFromStation.setBounds(55, 21, 55, 20);
        this.panel_1.add(this.txtFromStation);
        this.txtFromStation.setColumns(10);

        JLabel txtToStationl = new JLabel(ResManager.getString("RobTicket.txtToStation"));
        txtToStationl.setBounds(125, 25, 33, 12);
        this.panel_1.add(txtToStationl);
        txtToStationl.setHorizontalAlignment(4);
        this.txtToStation = new JTextField();
        //this.txtToStation.setToolTipText(ResManager.getString("RobTicket.txtToStationTip"));
        this.txtToStation.setBounds(160, 21, 55, 20);
        this.panel_1.add(this.txtToStation);
        this.txtToStation.setColumns(10);

        JLabel txtStartDatel = new JLabel(ResManager.getString("RobTicket.txtStartDate"));
        txtStartDatel.setBounds(230, 25, 63, 12);
        this.panel_1.add(txtStartDatel);
        txtStartDatel.setHorizontalAlignment(4);
        MaskFormatter mf = null;
        try {
            mf = new MaskFormatter("####-##-##");
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        this.txtStartDate = new JFormattedTextField(mf);
        //this.txtStartDate.setToolTipText(ResManager.getString("RobTicket.txtStartDateTip"));
        this.txtStartDate.setBounds(295, 21, 75, 20);
        this.panel_1.add(this.txtStartDate);
        this.txtStartDate.setColumns(10);

        JLabel boxoRangl = new JLabel(ResManager.getString("RobTicket.boxoRang"));
        boxoRangl.setBounds(20, 50, 68, 12);
        this.panel_1.add(boxoRangl);
        boxoRangl.setHorizontalAlignment(4);
        this.boxoRang = new JComboBox(Constants.trainRang.keySet().toArray());
        //this.boxoRang.setToolTipText(ResManager.getString("RobTicket.boxoRangTip"));
        this.boxoRang.setBounds(90, 46, 105, 20);
        this.panel_1.add(this.boxoRang);

        JLabel txtTrainCodel = new JLabel(ResManager.getString("RobTicket.txtTrainCode"));
        txtTrainCodel.setBounds(210, 50, 43, 12);
        this.panel_1.add(txtTrainCodel);
        txtTrainCodel.setHorizontalAlignment(4);
        this.txtTrainCode = new JTextField();
        //this.txtTrainCode.setToolTipText(ResManager.getString("RobTicket.txtTrainCodeTip"));
        this.txtTrainCode.setBounds(255, 46, 60, 20);
        this.panel_1.add(this.txtTrainCode);
        this.txtTrainCode.setColumns(10);

        JLabel boxoSeatTypel = new JLabel(ResManager.getString("RobTicket.boxoSeat"));
        boxoSeatTypel.setBounds(330, 50, 43, 12);
        this.panel_1.add(boxoSeatTypel);
        boxoSeatTypel.setHorizontalAlignment(4);
        this.boxoSeat = new JComboBox(Constants.trainSeatTypeMap.keySet().toArray());
        //this.boxoSeatType.setToolTipText(ResManager.getString("RobTicket.boxoSeatTypeTip"));
        this.boxoSeat.setBounds(375, 46, 75, 20);
        this.panel_1.add(this.boxoSeat);

        // 乘车人信息
        this.panel_2 = new JPanel();
        this.panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), ResManager.getString("RobTicket.panel_2BorderTitle"), 4, 2, null,
                new Color(0, 0, 0)));
        this.panel_2.setBounds(0, 150, 500, 160);
        this.frame.getContentPane().add(this.panel_2);
        this.panel_2.setLayout(null);

        // 联系人1
        JLabel txtUserIDl_1 = new JLabel(ResManager.getString("RobTicket.txtUserID"));
        txtUserIDl_1.setBounds(20, 25, 73, 12);
        this.panel_2.add(txtUserIDl_1);
        txtUserIDl_1.setHorizontalAlignment(4);
        this.txtUserID1 = new JTextField();
        //this.txtUserID1.setToolTipText(ResManager.getString("RobTicket.txtUserIDTip"));
        this.txtUserID1.setBounds(95, 21, 120, 20);
        this.panel_2.add(this.txtUserID1);
        this.txtUserID1.setColumns(10);

        JLabel txtNamel_1 = new JLabel(ResManager.getString("RobTicket.txtName"));
        txtNamel_1.setBounds(230, 25, 43, 12);
        this.panel_2.add(txtNamel_1);
        txtNamel_1.setHorizontalAlignment(4);
        this.txtName1 = new JTextField();
        //this.txtName1.setToolTipText(ResManager.getString("RobTicket.txtNameTip"));
        this.txtName1.setBounds(275, 21, 50, 20);
        this.panel_2.add(this.txtName1);
        this.txtName1.setColumns(10);

        JLabel txtPhonel_1 = new JLabel(ResManager.getString("RobTicket.txtPhone"));
        txtPhonel_1.setBounds(340, 25, 63, 12);
        this.panel_2.add(txtPhonel_1);
        txtPhonel_1.setHorizontalAlignment(4);
        try {
            mf = new MaskFormatter("###########");
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        this.txtPhone1 = new JFormattedTextField(mf);
        //this.txtPhone1.setToolTipText(ResManager.getString("RobTicket.txtPhoneTip"));
        this.txtPhone1.setBounds(405, 21, 80, 20);
        this.panel_2.add(this.txtPhone1);
        this.txtPhone1.setColumns(10);

        // 联系人2
        JLabel txtUserIDl_2 = new JLabel(ResManager.getString("RobTicket.txtUserID"));
        txtUserIDl_2.setBounds(20, 50, 73, 12);
        this.panel_2.add(txtUserIDl_2);
        txtUserIDl_2.setHorizontalAlignment(4);
        this.txtUserID2 = new JTextField();
        //this.txtUserID2.setToolTipText(ResManager.getString("RobTicket.txtUserIDTip"));
        this.txtUserID2.setBounds(95, 46, 120, 20);
        this.panel_2.add(this.txtUserID2);
        this.txtUserID2.setColumns(10);

        JLabel txtNamel_2 = new JLabel(ResManager.getString("RobTicket.txtName"));
        txtNamel_2.setBounds(230, 50, 43, 12);
        this.panel_2.add(txtNamel_2);
        txtNamel_2.setHorizontalAlignment(4);
        this.txtName2 = new JTextField();
        //this.txtName2.setToolTipText(ResManager.getString("RobTicket.txtNameTip"));
        this.txtName2.setBounds(275, 46, 50, 20);
        this.panel_2.add(this.txtName2);
        this.txtName2.setColumns(10);

        JLabel txtPhonel_2 = new JLabel(ResManager.getString("RobTicket.txtPhone"));
        txtPhonel_2.setBounds(340, 50, 63, 12);
        this.panel_2.add(txtPhonel_2);
        txtPhonel_2.setHorizontalAlignment(4);
        try {
            mf = new MaskFormatter("###########");
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        this.txtPhone2 = new JFormattedTextField(mf);
        //this.txtPhone2.setToolTipText(ResManager.getString("RobTicket.txtPhoneTip"));
        this.txtPhone2.setBounds(405, 50, 80, 20);
        this.panel_2.add(this.txtPhone2);
        this.txtPhone2.setColumns(10);

        // 联系人3
        JLabel txtUserIDl_3 = new JLabel(ResManager.getString("RobTicket.txtUserID"));
        txtUserIDl_3.setBounds(20, 75, 73, 12);
        this.panel_2.add(txtUserIDl_3);
        txtUserIDl_3.setHorizontalAlignment(4);
        this.txtUserID3 = new JTextField();
        //this.txtUserID3.setToolTipText(ResManager.getString("RobTicket.txtUserIDTip"));
        this.txtUserID3.setBounds(95, 71, 120, 20);
        this.panel_2.add(this.txtUserID3);
        this.txtUserID3.setColumns(10);

        JLabel txtNamel_3 = new JLabel(ResManager.getString("RobTicket.txtName"));
        txtNamel_3.setBounds(230, 75, 43, 12);
        this.panel_2.add(txtNamel_3);
        txtNamel_3.setHorizontalAlignment(4);
        this.txtName3 = new JTextField();
        //this.txtName3.setToolTipText(ResManager.getString("RobTicket.txtNameTip"));
        this.txtName3.setBounds(275, 71, 50, 20);
        this.panel_2.add(this.txtName3);
        this.txtName3.setColumns(10);

        JLabel txtPhonel_3 = new JLabel(ResManager.getString("RobTicket.txtPhone"));
        txtPhonel_3.setBounds(340, 75, 63, 12);
        this.panel_2.add(txtPhonel_3);
        txtPhonel_3.setHorizontalAlignment(4);
        try {
            mf = new MaskFormatter("###########");
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        this.txtPhone3 = new JFormattedTextField(mf);
        //this.txtPhone3.setToolTipText(ResManager.getString("RobTicket.txtPhoneTip"));
        this.txtPhone3.setBounds(405, 71, 80, 20);
        this.panel_2.add(this.txtPhone3);
        this.txtPhone3.setColumns(10);

        // 联系人4
        JLabel txtUserIDl_4 = new JLabel(ResManager.getString("RobTicket.txtUserID"));
        txtUserIDl_4.setBounds(20, 100, 73, 12);
        this.panel_2.add(txtUserIDl_4);
        txtUserIDl_4.setHorizontalAlignment(4);
        this.txtUserID4 = new JTextField();
        //this.txtUserID4.setToolTipText(ResManager.getString("RobTicket.txtUserIDTip"));
        this.txtUserID4.setBounds(95, 96, 120, 20);
        this.panel_2.add(this.txtUserID4);
        this.txtUserID4.setColumns(10);

        JLabel txtNamel_4 = new JLabel(ResManager.getString("RobTicket.txtName"));
        txtNamel_4.setBounds(230, 100, 43, 12);
        this.panel_2.add(txtNamel_4);
        txtNamel_4.setHorizontalAlignment(4);
        this.txtName4 = new JTextField();
        //this.txtName4.setToolTipText(ResManager.getString("RobTicket.txtNameTip"));
        this.txtName4.setBounds(275, 96, 50, 20);
        this.panel_2.add(this.txtName4);
        this.txtName4.setColumns(10);

        JLabel txtPhonel_4 = new JLabel(ResManager.getString("RobTicket.txtPhone"));
        txtPhonel_4.setBounds(340, 100, 63, 12);
        this.panel_2.add(txtPhonel_4);
        txtPhonel_4.setHorizontalAlignment(4);
        try {
            mf = new MaskFormatter("###########");
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        this.txtPhone4 = new JFormattedTextField(mf);
        //this.txtPhone4.setToolTipText(ResManager.getString("RobTicket.txtPhoneTip"));
        this.txtPhone4.setBounds(405, 96, 80, 20);
        this.panel_2.add(this.txtPhone4);
        this.txtPhone4.setColumns(10);

        // 联系人5
        JLabel txtUserIDl_5 = new JLabel(ResManager.getString("RobTicket.txtUserID"));
        txtUserIDl_5.setBounds(20, 125, 73, 12);
        this.panel_2.add(txtUserIDl_5);
        txtUserIDl_5.setHorizontalAlignment(4);
        this.txtUserID5 = new JTextField();
        //this.txtUserID5.setToolTipText(ResManager.getString("RobTicket.txtUserIDTip"));
        this.txtUserID5.setBounds(95, 121, 120, 20);
        this.panel_2.add(this.txtUserID5);
        this.txtUserID5.setColumns(10);

        JLabel txtNamel_5 = new JLabel(ResManager.getString("RobTicket.txtName"));
        txtNamel_5.setBounds(230, 125, 43, 12);
        this.panel_2.add(txtNamel_5);
        txtNamel_5.setHorizontalAlignment(4);
        this.txtName5 = new JTextField();
        //this.txtName5.setToolTipText(ResManager.getString("RobTicket.txtNameTip"));
        this.txtName5.setBounds(275, 121, 50, 20);
        this.panel_2.add(this.txtName5);
        this.txtName5.setColumns(10);

        JLabel txtPhonel_5 = new JLabel(ResManager.getString("RobTicket.txtPhone"));
        txtPhonel_5.setBounds(340, 125, 63, 12);
        this.panel_2.add(txtPhonel_5);
        txtPhonel_5.setHorizontalAlignment(4);
        try {
            mf = new MaskFormatter("###########");
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        this.txtPhone5 = new JFormattedTextField(mf);
        //this.txtPhone5.setToolTipText(ResManager.getString("RobTicket.txtPhoneTip"));
        this.txtPhone5.setBounds(405, 121, 80, 20);
        this.panel_2.add(this.txtPhone5);
        this.txtPhone5.setColumns(10);
        

        // 限制条件
        this.panel_3 = new JPanel();
        this.panel_3.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), ResManager.getString("RobTicket.panel_3BorderTitle"), 4, 2, null,
                new Color(0, 0, 0)));
        this.panel_3.setBounds(0, 320, 500, 120);
        this.frame.getContentPane().add(this.panel_3);
        this.panel_3.setLayout(null);
        // 开始时间段
        JLabel txtBeginTimel = new JLabel(ResManager.getString("RobTicket.txtBeginTime"));
        txtBeginTimel.setBounds(20, 25, 83, 12);
        this.panel_3.add(txtBeginTimel);
        txtBeginTimel.setHorizontalAlignment(4);
        this.txtBeginTime = new JTextField();
        //this.txtBeginTime.setToolTipText(ResManager.getString("RobTicket.txtBeginTimeTip"));
        this.txtBeginTime.setBounds(105, 21, 40, 20);
        this.panel_3.add(this.txtBeginTime);
        this.txtBeginTime.setColumns(2);
        // 结束时间段
        JLabel txtEndTimel = new JLabel(ResManager.getString("RobTicket.txtEndTime"));
        txtEndTimel.setBounds(155, 25, 83, 12);
        this.panel_3.add(txtEndTimel);
        txtEndTimel.setHorizontalAlignment(4);
        this.txtEndTime = new JTextField();
        //this.txtEndTime.setToolTipText(ResManager.getString("RobTicket.txtEndTimeTip"));
        this.txtEndTime.setBounds(240, 21, 40, 20);
        this.panel_3.add(this.txtEndTime);
        this.txtEndTime.setColumns(2);
        // 车次匹配过滤
        JLabel txtTrainCodeMatchl = new JLabel(ResManager.getString("RobTicket.txtTrainCodeMatch"));
        txtTrainCodeMatchl.setBounds(290, 25, 63, 12);
        this.panel_3.add(txtTrainCodeMatchl);
        txtTrainCodeMatchl.setHorizontalAlignment(4);
        this.txtTrainCodeMatch = new JTextField();
        //this.txtTrainCodeMatch.setToolTipText(ResManager.getString("RobTicket.txtTrainCodeMatchTip"));
        this.txtTrainCodeMatch.setBounds(355, 21, 80, 20);
        this.panel_3.add(this.txtTrainCodeMatch);
        this.txtTrainCodeMatch.setColumns(8);
        
        // 黑名单(车次)
        JLabel txtBlackTrainCodesl = new JLabel(ResManager.getString("RobTicket.txtBlackTrainCodes"));
        txtBlackTrainCodesl.setBounds(20, 50, 83, 12);
        this.panel_3.add(txtBlackTrainCodesl);
        txtBlackTrainCodesl.setHorizontalAlignment(4);
        this.txtBlackTrainCodes = new JTextField();
        //this.txtBlackTrainCodes.setToolTipText(ResManager.getString("RobTicket.txtBlackTrainCodesTip"));
        this.txtBlackTrainCodes.setBounds(105, 46, 350, 20);
        this.panel_3.add(this.txtBlackTrainCodes);
        this.txtBlackTrainCodes.setColumns(30);
        
        // 座次匹配
        JLabel txtSeatTypesl = new JLabel(ResManager.getString("RobTicket.txtSeatTypes"));
        txtSeatTypesl.setBounds(20, 75, 83, 12);
        this.panel_3.add(txtSeatTypesl);
        txtSeatTypesl.setHorizontalAlignment(4);
        this.txtSeatTypes = new JTextField();
        //this.txtSeatTypes.setToolTipText(ResManager.getString("RobTicket.txtSeatTypesTip"));
        this.txtSeatTypes.setBounds(105, 71, 350, 20);
        this.panel_3.add(this.txtSeatTypes);
        this.txtSeatTypes.setColumns(30);
        

        this.boxkDFirst = new JCheckBox(ResManager.getString("RobTicket.boxkDFirst"));
        //this.boxkDFirst.setToolTipText(ResManager.getString("RobTicket.boxkDFirstTip"));
        this.boxkDFirst.setBounds(20, 450, 80, 20);
        this.frame.getContentPane().add(this.boxkDFirst);

        this.boxkHardSleepFirst = new JCheckBox(ResManager.getString("RobTicket.boxkHardSleepFirst"));
        //this.boxkHardSleepFirst.setToolTipText(ResManager.getString("RobTicket.boxkHardSleepFirstTip"));
        this.boxkHardSleepFirst.setBounds(110, 450, 80, 20);
        this.frame.getContentPane().add(this.boxkHardSleepFirst);

        this.boxkIsAuto = new JCheckBox(ResManager.getString("RobTicket.boxkIsAuto"));
        //this.boxkIsAuto.setToolTipText(ResManager.getString("RobTicket.boxkIsAutoTip"));
        this.boxkIsAuto.setBounds(200, 450, 110, 20);
        this.frame.getContentPane().add(this.boxkIsAuto);

        this.boxkEnforce = new JCheckBox(ResManager.getString("RobTicket.boxkEnforce"));
        //this.boxkEnforce.setToolTipText(ResManager.getString("RobTicket.boxkEnforceTip"));
        this.boxkEnforce.setBounds(320, 450, 80, 20);
        this.frame.getContentPane().add(this.boxkEnforce); // 去掉锁定车次

        this.btnSORE = new JButton(ResManager.getString("RobTicket.btnSORE"));
        //this.btnSORE.setToolTipText(ResManager.getString("RobTicket.btnSORETip"));
        this.btnSORE.setBounds(410, 450, 75, 20);
        this.btnSORE.addActionListener(new StartAction());
        this.frame.getContentPane().add(this.btnSORE);
        

        JScrollPane scrollPane = new JScrollPane();
        scrollPane.setViewportBorder(new BevelBorder(1, null, null, null, null));
        scrollPane.setHorizontalScrollBarPolicy(31);
        scrollPane.setBounds(0, 480, 500, 180);
        this.frame.getContentPane().add(scrollPane);

        this.textArea = new JTextArea();
        scrollPane.setViewportView(this.textArea);
        this.textArea.setText(ResManager.getString("RobTicket.textAreaContent"));
        this.textArea.setEditable(false);
        this.textArea.setLineWrap(true);

        readUserInfo();
        //refreshRandCode(4);
    }

    public void reset(boolean isEnd) {
        if (isEnd) {
            this.btnSORE.setText(ResManager.getString("RobTicket.btnSORE"));
            if (this.logic != null) {
                this.setLoginSuc(this.logic.isLoginSuc());
                this.logic.setEnd(true);
            }
            // refreshRandCode(4);
        } /*else {
			Util.waitMoment(Config.getSleepTime() + (long) (Math.random() * Config.getSleepTime() / 3));// 加入随机数
        }*/
        resetUserInfo();
    }

    public void resetUserInfo() {
        if (Constants.ISTRAINDFIRST) {
            Constants.setTrainPriority(Constants.TRAIN_D, 30);
            Constants.setTrainPriority(Constants.TRAIN_G, 40);
        } else {
            Constants.setTrainPriority(Constants.TRAIN_D, 70);
            Constants.setTrainPriority(Constants.TRAIN_G, 80);
        }
        Constants.ISAUTOCODE = this.boxkIsAuto.isSelected();
        Constants.ISTRAINDFIRST = this.boxkDFirst.isSelected();
        Constants.ISHARDSLEEPFIRST = this.boxkHardSleepFirst.isSelected();
        Constants.ISENFORCE = this.boxkEnforce.isSelected();
        this.ui.setBoxkDFirst(Constants.ISTRAINDFIRST);
        this.ui.setBoxkHardSleepFirst(Constants.ISHARDSLEEPFIRST);
        this.ui.setBoxkIsAuto(Constants.ISAUTOCODE);
        this.ui.setBoxkEnforce(Constants.ISENFORCE);
        this.ui.setUsername(getUsername());
        this.ui.setPassword(getPassword());
        this.ui.setFrom_station_telecode_name(getFromCity());
        this.ui.setTo_station_telecode_name(getToCity());
        this.ui.setTrain_date(getTrainDate());
        this.ui.setRound_start_time_str(getRangDate());
        this.ui.setTrain_code(getTrainCode());
        this.ui.setTrain_no(getTrainNo());
        this.ui.setSeattype_num(getSeatNum());
        this.ui.setCardno1(getUserID1());
        this.ui.setName1(getName1());
        this.ui.setMobileno1(getMobileno1());
        this.ui.setCardno2(getUserID2());
        this.ui.setName2(getName2());
        this.ui.setMobileno2(getMobileno2());
        this.ui.setCardno3(getUserID3());
        this.ui.setName3(getName3());
        this.ui.setMobileno3(getMobileno3());
        this.ui.setCardno4(getUserID4());
        this.ui.setName4(getName4());
        this.ui.setMobileno4(getMobileno4());
        this.ui.setCardno5(getUserID5());
        this.ui.setName5(getName5());
        this.ui.setMobileno5(getMobileno5());
        this.ui.setBeginTime(getBeginTime());
        this.ui.setEndTime(getEndTime());
        this.ui.setTrainCodeMatch(getTrainCodeMatch());
        this.ui.setBlackTrainCodes(getBlackTrainCodes());
        this.ui.setSeatTypes(getSeatTypes());
        this.ui.setSeat(getSeat());
        this.ui.setTicket("1");
        this.ui.setCardtype("1");
        this.ui.setId_mode("Y");
        writeUserInfo(this.ui);
    }

    protected void action() {
        this.btnSORE.setText(ResManager.getString("RobTicket.btnSORE.end"));
        this.textArea.setText("");
        String username = getUsername();
        String password = getPassword();
        // String randCode = getRandCode();
        String fromCity = getFromCity();
        String toCity = getToCity();
        String trainDate = getTrainDate();
        if (username.isEmpty() || password.isEmpty() || fromCity.isEmpty() || toCity.isEmpty() || trainDate.isEmpty()) {// || randCode.isEmpty()
            JOptionPane.showMessageDialog(null, ResManager.getString("RobTicket.JOptionPane"));
            reset(true);
            return;
        }
        resetUserInfo();
        String userName = "";
        String cookies = "";
        if(this.logic != null) {
        	userName = this.logic.getUsername();
        	cookies = this.logic.getCookies();
        }
        this.logic = new LogicThread(this.ui, this.client, this, this.isLoginSuc(), userName, cookies);
        this.logic.start();
    }

    private void writeUserInfo(UserInfo ui) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File("ui")));
            oos.writeObject(ui);
            oos.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null)
                    oos.close();
            } catch (Exception e) {
            }
        }
    }

    private void readUserInfo() {
        ObjectInputStream ois = null;
        try {
            boolean fromConfig = true;
            File uif = new File("ui");
            if (uif.exists()) {
                ois = new ObjectInputStream(new FileInputStream(uif));
                UserInfo ui = (UserInfo) ois.readObject();
                if (ui != null) {
                    fromConfig = false;
                    this.txtUsername.setText(ui.getUsername());
                    this.txtPassword.setText(ui.getPassword());

                    this.txtFromStation.setText(ui.getFrom_station_telecode_name());
                    this.txtToStation.setText(ui.getTo_station_telecode_name());
                    this.txtStartDate.setText(ui.getTrain_date());
                    this.boxoRang.setSelectedItem(ui.getRound_start_time_str());
                    this.txtTrainCode.setText(ui.getTrain_code());
                    this.trainNo = ui.getTrain_no();
                    this.seatNum = ui.getSeattype_num();

                    this.txtUserID1.setText(ui.getCardno1());
                    this.txtName1.setText(ui.getName1());
                    this.txtPhone1.setText(ui.getMobileno1());
                    this.txtUserID2.setText(ui.getCardno2());
                    this.txtName2.setText(ui.getName2());
                    this.txtPhone2.setText(ui.getMobileno2());
                    this.txtUserID3.setText(ui.getCardno3());
                    this.txtName3.setText(ui.getName3());
                    this.txtPhone3.setText(ui.getMobileno3());
                    this.txtUserID4.setText(ui.getCardno4());
                    this.txtName4.setText(ui.getName4());
                    this.txtPhone4.setText(ui.getMobileno4());
                    this.txtUserID5.setText(ui.getCardno5());
                    this.txtName5.setText(ui.getName5());
                    this.txtPhone5.setText(ui.getMobileno5());
                    this.txtBeginTime.setText(ui.getBeginTime());
                    this.txtEndTime.setText(ui.getEndTime());
                    this.txtTrainCodeMatch.setText(ui.getTrainCodeMatch());
                    this.txtBlackTrainCodes.setText(ui.getBlackTrainCodes());
                    this.txtSeatTypes.setText(ui.getSeatTypes());
                    this.boxoSeat.setSelectedItem(Constants.getTrainSeatName(ui.getSeat()));

                    this.boxkDFirst.setSelected(ui.isBoxkDFirst());
                    this.boxkHardSleepFirst.setSelected(ui.isBoxkHardSleepFirst());
                    this.boxkIsAuto.setSelected(ui.isBoxkIsAuto());
                    this.boxkEnforce.setSelected(ui.isBoxkEnforce());
                }
            }
            if (fromConfig) {
                this.boxkDFirst.setSelected(true);
                this.boxkHardSleepFirst.setSelected(true);
                this.boxkIsAuto.setSelected(true);
                this.boxkEnforce.setSelected(false);
            }
        } catch (Exception e) {
        } finally {
            try {
                if (ois != null)
                    ois.close();
            } catch (IOException e) {
            }
        }
    }

    public void console(String content) {
        this.textArea.append(Util.formatInfo(content));
        this.textArea.setCaretPosition(this.textArea.getText().length());
    }

    public JFrame getFrame() {
        return this.frame;
    }

    public void refreshRandCode(final int len) {
        new Thread() {
            public void run() {
                byte[] image = RobTicket.this.client.getCodeByte(Constants.Login_Code_URL);
                String randCodeByRob = OCR.read(image);
                //RobTicket.this.lblRc.setIcon(new ImageIcon(image));
                //RobTicket.this.txtRandCode.setText(randCodeByRob);
            }
        }.start();
    }

    protected class ExitThread extends Thread {
        protected ExitThread() {
        }

        public void run() {
            RobTicket.this.httpClient.getConnectionManager().shutdown();
        }
    }

    class StartAction implements ActionListener {
        StartAction() {
        }

        public void actionPerformed(ActionEvent e) {
            JButton btn = (JButton) e.getSource();
            if (ResManager.getString("RobTicket.btnSORE").equals(btn.getText()))
                RobTicket.this.action();
            else
                RobTicket.this.reset(true);
        }
    }

    class RandCodeListener extends MouseAdapter {
        RandCodeListener() {
        }

        public void mouseClicked(MouseEvent e) {
            RobTicket.this.refreshRandCode(4);
        }
    }

    private String trainNo;
    private String seatNum;

    private boolean isLoginSuc = false;

    public String getUsername() {
        return this.txtUsername.getText().trim();
    }

    public String getPassword() {
        return this.txtPassword.getText().trim();
    }

    //public String getRandCode() {
    //    return this.txtRandCode.getText().trim();
    //}

    public String getToCity() {
        return this.txtToStation.getText().trim();
    }

    public String getFromCity() {
        return this.txtFromStation.getText().trim();
    }

    public String getTrainDate() {
        return this.txtStartDate.getText().trim();
    }

    public String getRangDate() {
        String key = (String) this.boxoRang.getSelectedItem();
        String rangDate = Constants.getTrainRang(key);
        return rangDate;
    }

    public String getTrainCode() {
        return this.txtTrainCode.getText().trim();
    }

    public String getName1() {
        return this.txtName1.getText().trim();
    }

    public String getUserID1() {
        return this.txtUserID1.getText().trim();
    }

    public String getMobileno1() {
        return this.txtPhone1.getText().trim();
    }

    public String getName2() {
        return this.txtName2.getText().trim();
    }

    public String getUserID2() {
        return this.txtUserID2.getText().trim();
    }

    public String getMobileno2() {
        return this.txtPhone2.getText().trim();
    }

    public String getName3() {
        return this.txtName3.getText().trim();
    }

    public String getUserID3() {
        return this.txtUserID3.getText().trim();
    }

    public String getMobileno3() {
        return this.txtPhone3.getText().trim();
    }

    public String getName4() {
        return this.txtName4.getText().trim();
    }

    public String getUserID4() {
        return this.txtUserID4.getText().trim();
    }

    public String getMobileno4() {
        return this.txtPhone4.getText().trim();
    }

    public String getName5() {
        return this.txtName5.getText().trim();
    }

    public String getUserID5() {
        return this.txtUserID5.getText().trim();
    }

    public String getMobileno5() {
        return this.txtPhone5.getText().trim();
    }

    public String getBeginTime() {
        return this.txtBeginTime.getText().trim();
    }

    public String getEndTime() {
        return this.txtEndTime.getText().trim();
    }

    public String getTrainCodeMatch() {
        return this.txtTrainCodeMatch.getText().trim();
    }

    public String getBlackTrainCodes() {
        return this.txtBlackTrainCodes.getText().trim();
    }

	public void setBlackTrainCodes(String blackTrainCodes) {
		this.txtBlackTrainCodes.setText(blackTrainCodes);
	}

    public String getSeatTypes() {
        return this.txtSeatTypes.getText().trim();
    }

	public void setSeatTypes(String SeatTypes) {
		this.txtSeatTypes.setText(SeatTypes);
	}

    public String getSeat() {
        String key = (String) this.boxoSeat.getSelectedItem();
        String seat = Constants.getTrainSeatType(key);
        return seat;
    }

    public String getTrainNo() {
        return trainNo;
    }

    public String getSeatNum() {
        return seatNum;
    }

    public boolean isLoginSuc() {
        return isLoginSuc;
    }

    public void setLoginSuc(boolean isLoginSuc) {
        this.isLoginSuc = isLoginSuc;
    }

	public HttpClient getHttpClient() {
		return httpClient;
	}

	public void setHttpClient(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	public TrainClient getClient() {
		return client;
	}

	public void setClient(TrainClient client) {
		this.client = client;
	}

}
